package ch.app.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import ch.app.dao.GetCashDao;
import ch.app.dao.UserDao;
import ch.app.redis.RedisUtil;

/**
 * 
 * @author Yin Yong Gang
 * Date:
 * Description: 该接口只要是测试redis得增删改查
 * 
 *  1.redisTemplate.opsForValue();//操作字符串
    2.redisTemplate.opsForHash(); //操作hash
	3.redisTemplate.opsForList(); //操作list
	4.redisTemplate.opsForSet();  //操作set
	5.redisTemplate.opsForZSet(); //操作有序set
 */

@RestController
@RequestMapping("/redis")
public class RedisController {

	@Resource
	private UserDao userDao;
	
	@Resource
	private GetCashDao getCashDao;
	
	@Resource
	private RedisUtil redisUtil;
	
	@Autowired
	private RedisTemplate<String,String> redisTemplate;
	
	
	//hash
	@RequestMapping(value = "/saveHash", method = RequestMethod.POST)
	public void redisAll(@RequestBody String value) {
		//增1
		redisTemplate.opsForHash().put("j-key", "j-hashkey", value);
		System.out.println("------opsForHash---put-");
		//查1
		String getValue = (String) redisTemplate.opsForHash().get("j-key", "j-hashkey");
		System.out.println("------opsForHash---get->" + getValue);
		
		//----------------------------------------------------------
		
		//增2
		Map<String, String> maps = new HashMap<String, String>();
		maps.put("k-001", "v-001");
		maps.put("k-002", "v-002");
		maps.put("k-003", "v-003");
		redisTemplate.opsForHash().putAll("j-keymaps", maps);
		
		//查2
		Map<Object, Object> mapsGet = redisTemplate.opsForHash().entries("j-keymaps");
		Iterator<Map.Entry<Object, Object>> iterator =  mapsGet.entrySet().iterator();
		while(iterator.hasNext()) {
			Entry<Object, Object> valuex = iterator.next();
			System.out.println("------opsForHash---entries-> key:" + valuex.getKey() + " value : " + valuex.getValue());
		}
		
		//删 (删除一个数组 / 或者单个去删除)
		redisTemplate.opsForHash().delete("j-keymaps", new String[]{"k-003", "k-002"});
	}
	
	/**
	 *	字符串的操作 
	 * @param value
	 */
	@RequestMapping(value = "/saveString", method = RequestMethod.POST)
	public void saveString(@RequestBody String value) {
		//增
		redisTemplate.opsForValue().set("j-str", value, 10, TimeUnit.SECONDS);//10秒后数据会从内存消失
		
		//查
		String getValue = redisTemplate.opsForValue().get("j-str");
		System.out.println("------opsForValue---get->" + getValue);
		
		Long longs = redisTemplate.opsForValue().size("j-str");
		System.out.println("------opsForValue---size->" + longs);
	}
	
	@RequestMapping(value = "/findByString", method = RequestMethod.POST)
	public void findByString(@RequestBody String value) {
		String getValue = redisTemplate.opsForValue().get("j-str");
		System.out.println("------opsForValue---get->" + getValue);
	}
	
	/**
	 * 1. list不会覆盖增加
	 * 2. leftPop 带pop的方法会查询和删除.谨慎使用
	 * 3. range("j-listkey", 0, -1); 查找全部list值
	 */
	@RequestMapping(value = "/saveList", method = RequestMethod.POST)
	public void saveList(@RequestBody String value) {
		//增
		List<String> list1 = new ArrayList<String>();
		list1.add("a1");
		list1.add("a2");
		list1.add("a3");
		redisTemplate.opsForList().leftPushAll("j-listkey", list1);
		//String resultList1 = redisTemplate.opsForList().leftPop("j-listkey");
		//String resultList11 = redisTemplate.opsForList().rightPop("j-listkey");
		//System.out.println("resultList1 ---> left:" + resultList1 + "right: " + resultList11);
		
		System.out.println(redisTemplate.opsForList().size("j-listkey"));
		
		//查
		List<String> lists = redisTemplate.opsForList().range("j-listkey", 0, -1);//get the values from index 0 to last one(-1)
		for (String str : lists) {
			System.out.println("----range API----: " + str);	
		}
		
		//删(删除第0个a1)
		redisTemplate.opsForList().remove("j-listkey", 0, "a1");
		
		//查
		List<String> listss = redisTemplate.opsForList().range("j-listkey", 0, -1);//get the values from index 0 to last one(-1)
		for (String str : listss) {
			System.out.println("----after delete----: " + str);	
		}
	}
	
	@RequestMapping(value = "/saveSet", method = RequestMethod.POST)
	public void saveSet(@RequestBody String value) {
		
		//增
		Set<String> sets = new HashSet<String>();
		sets.add("set1");
		sets.add("set2");
		sets.add("set3");
		redisTemplate.opsForSet().add("j-set", "set1");
		redisTemplate.opsForSet().add("j-set", "set2");
		redisTemplate.opsForSet().add("j-set", "set3");
		
		//查全部
		Set<String> seta = redisTemplate.opsForSet().members("j-set");
		System.out.println("----set find result----: " + seta);	
		
		//删
		Long remove = redisTemplate.opsForSet().remove("j-set", "set1");
		System.out.println("----remove----: " + remove);
		
		//判断是否是
		Boolean boo = redisTemplate.opsForSet().isMember("j-set", "set2");
		System.out.println("----if exit----: " + boo);
		
		Set<String> setafter = redisTemplate.opsForSet().members("j-set");
		System.out.println("----set find result----: " + setafter);
	}
}
